/*
 * ResponsibilityValueDisplay.java
 *
 * Created on 12 January 2004, 05:07
 * Copyright (C) 2004, Velocityme Partnership
 */

package com.velocityme.client.gui.responsibility;

import com.velocityme.client.gui.ValueListModel;
import com.velocityme.client.gui.node.contactable.ContactableTreeCellRenderer;
import com.velocityme.interfaces.ContactablePK;
import com.velocityme.valueobjects.ContactableValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author  Robert Crida Work
 */
public class ResponsibilityValueDisplay extends javax.swing.JPanel {
    
    private ValueListModel m_assignedListModel;
//    private ValueListModel m_unassignedListModel;
    private TreeModel m_allTreeModel;

    /** Creates new form ResponsibilityValueDisplay */
    public ResponsibilityValueDisplay() {
        initComponents();
    }
    
    public void setAssignedValues(Collection p_assignedValues, TreeModel p_allTreeModel) {
        m_assignedListModel.setItems(p_assignedValues);
        m_allTreeModel = p_allTreeModel;
        if (m_allTreeModel != null) {
            jTreeUnassigned.setModel(m_allTreeModel);
        }
    }
    
    public Collection getAssignedValues() {
        return m_assignedListModel.getCollection();
    }
    
    public ContactablePK[] getAssignedPKs() {
        ContactablePK[] contactablePKs = new ContactablePK[m_assignedListModel.getCollection().size()];
        Iterator i = m_assignedListModel.getCollection().iterator();
        int index = 0;
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            contactablePKs[index++] = contactableValue.getPrimaryKey();
        }
        return contactablePKs;
    }
    
    public void setEditable(boolean p_editable) {
        jListAssigned.setEnabled(p_editable);
        jTreeUnassigned.setEnabled(p_editable);
        jButtonAdd.setVisible(p_editable);
        jButtonRemove.setVisible(p_editable);
        jScrollPaneUnassigned.setVisible(p_editable);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        jScrollPaneUnassigned = new javax.swing.JScrollPane();
        jTreeUnassigned = new javax.swing.JTree();
        jTreeUnassigned.setCellRenderer(new ContactableTreeCellRenderer());
        jButtonAdd = new javax.swing.JButton();
        jButtonRemove = new javax.swing.JButton();
        jScrollPaneAssigned = new javax.swing.JScrollPane();
        m_assignedListModel = new ValueListModel();
        jListAssigned = new javax.swing.JList(m_assignedListModel);

        setLayout(new java.awt.GridBagLayout());

        jScrollPaneUnassigned.setPreferredSize(new java.awt.Dimension(259, 66));
        jTreeUnassigned.setRootVisible(false);
        jTreeUnassigned.setShowsRootHandles(true);
        jScrollPaneUnassigned.setViewportView(jTreeUnassigned);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneUnassigned, gridBagConstraints);

        jButtonAdd.setText("Add >>");
        jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButtonAdd, gridBagConstraints);

        jButtonRemove.setText("<< Remove");
        jButtonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRemoveActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weighty = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jButtonRemove, gridBagConstraints);

        jScrollPaneAssigned.setPreferredSize(new java.awt.Dimension(259, 66));
        jScrollPaneAssigned.setViewportView(jListAssigned);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(jScrollPaneAssigned, gridBagConstraints);

    }//GEN-END:initComponents

    private void jButtonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRemoveActionPerformed
        // Add your handling code here:
        Object[] remove = jListAssigned.getSelectedValues();
        m_assignedListModel.remove(remove);  
    }//GEN-LAST:event_jButtonRemoveActionPerformed

    private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddActionPerformed
        // Add your handling code here:
        TreePath[] addPaths = jTreeUnassigned.getSelectionPaths();
        Object[] add = new Object[addPaths.length];
        for (int i = 0; i < addPaths.length; i++) {
            add[i] = ((DefaultMutableTreeNode)addPaths[i].getLastPathComponent()).getUserObject();
        }
        m_assignedListModel.add(add);
    }//GEN-LAST:event_jButtonAddActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdd;
    private javax.swing.JButton jButtonRemove;
    private javax.swing.JList jListAssigned;
    private javax.swing.JScrollPane jScrollPaneAssigned;
    private javax.swing.JScrollPane jScrollPaneUnassigned;
    private javax.swing.JTree jTreeUnassigned;
    // End of variables declaration//GEN-END:variables
    
}
